﻿using B.S.BaseData.ErrorCode;
using B.S.BaseData.Manage.Api.Read.Apilaction.Command.ReportlossesModelCommand;
using B.S.BaseData.Manage.Api.Read.DTO;
using B.S.BaseData.Manage.Domain.Entities;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace B.S.BaseData.Manage.Api.Read.Controllers
{
    /// <summary>
    /// 报损
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ReportlossesController : ControllerBase
    {
        private readonly IMediator mediator;

        public ReportlossesController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 报损管理分页显示
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>取消</returns>
        [HttpGet]
        public async Task<ApiReqult<ApIPageing<ReportlossesModelDTO>>> ReportlossesQuser([FromQuery] ReportlossesQuserModelCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 报损id查询反填
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>取消</returns>
        [HttpGet]
        public async Task<ApiReqult<List<ReportlossesModel>>> ReportlossesFile([FromQuery] ReportlossesFileModelCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 根据报损编号查询
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>取消</returns>
        [HttpGet]
        public async Task<ApiReqult<List<ReportlossesModel>>> ReportlossesBate([FromQuery] ReportlossesBateModelCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 根据id批量查询
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>取消</returns>
        [HttpGet]
        public Task<ApiReqult<List<ReportlossesModel>>> ReportlossesExport([FromQuery] ReportlossesExportModelCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="commodityIds"></param>
        [HttpGet]
        public async Task<IActionResult> ExportOrderList([FromQuery] List<int> id)
        {
            if (id == null || !id.Any())
            {
                return BadRequest("请提供报损记录 ID 列表。");
            }

            var request = new ReportlossesExportModelCommand { Id = id };
            var result = await ReportlossesExport(request);

            if (result.Data?.Any() != true)
                return NotFound("未找到报损数据。");

            using IWorkbook book = new XSSFWorkbook();
            ISheet sheet = book.CreateSheet("报损列表");
            IRow headerRow = sheet.CreateRow(0);

            // 设置表头
            string[] headers = { "报损主键", "报损单号", "报损类型", "关联订单号", "制单人", "制单时间", "备注", "产品名称", "产品编码", "规格", "批次", "单价", "报损数量", "仓库(原库位)", "当前库位", "审核状态", "审核人", "审核原因", "审核时间", "逻辑删除列" };
            for (int i = 0; i < headers.Length; i++)
                headerRow.CreateCell(i).SetCellValue(headers[i]);

            // 填充数据行
            int rowIndex = 1;
            foreach (var item in result.Data) // 假设result.Data是ReportlossesModel类型集合
            {
                IRow row = sheet.CreateRow(rowIndex++);

                // 报损主键
                row.CreateCell(0).SetCellValue(item.Id);

                // 报损单号
                row.CreateCell(1).SetCellValue(item.Lossreportnumber ?? "");

                // 报损类型
                row.CreateCell(2).SetCellValue(item.Thetypeoflossreport ?? "");

                // 关联订单号
                row.CreateCell(3).SetCellValue(item.Associatetheordernumber ?? "");

                // 制单人
                row.CreateCell(4).SetCellValue(item.Makeasingle ?? "");

                // 制单时间
                row.CreateCell(5).SetCellValue(item.Thetimewhentheorderwasmade?.ToString("yyyy-MM-dd HH:mm:ss") ?? "");

                // 备注
                row.CreateCell(6).SetCellValue(item.Remark ?? "");

                // 产品名称
                row.CreateCell(7).SetCellValue(item.Productname ?? "");

                // 产品编码
                row.CreateCell(8).SetCellValue(item.Productcode ?? "");

                // 规格
                row.CreateCell(9).SetCellValue(item.Specification ?? "");

                // 批次
                row.CreateCell(10).SetCellValue(item.Batch ?? "");

                // 单价
                row.CreateCell(11).SetCellValue(item.Unitprice ?? "");

                // 报损数量
                row.CreateCell(12).SetCellValue(item.Reportedlossquantity ?? "");

                // 仓库(原库位)
                row.CreateCell(13).SetCellValue(item.Warehouse ?? "");

                // 当前库位
                row.CreateCell(14).SetCellValue(item.Thecurrentlocation ?? "");

                // 审核状态
                row.CreateCell(15).SetCellValue(item.Reviewstatus.HasValue ? item.Reviewstatus.Value.ToString() : "");

                // 审核人
                row.CreateCell(16).SetCellValue(item.Reviewers ?? "");

                // 审核原因
                row.CreateCell(17).SetCellValue(item.Reasonforreview ?? "");

                // 审核时间
                row.CreateCell(18).SetCellValue(item.Reviewtime?.ToString("yyyy-MM-dd HH:mm:ss") ?? "");

                // 逻辑删除列
                row.CreateCell(19).SetCellValue(item.IsDelete.HasValue ? item.IsDelete.Value.ToString() : "0");
            }

            using var ms = new MemoryStream();
            book.Write(ms);
            byte[] buffer = ms.ToArray();

            return File(buffer, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "报损记录列表.xlsx");
        }
    }
}
